<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="meetingName">
        <el-input
          v-model="queryParams.meetingName"
          placeholder="请输入会议室名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="meetingNumbering">
        <el-input
          v-model="queryParams.meetingNumbering"
          placeholder="请输入会议编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="特殊设施说明" prop="specialFacilitiesDescription">
        <el-input
          v-model="queryParams.specialFacilitiesDescription"
          placeholder="请输入会议室内的特殊设施说明"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置" prop="meetingLocation">
        <el-input
          v-model="queryParams.meetingLocation"
          placeholder="请输入会议室位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容纳总数" prop="maxCapacity">
        <el-input
          v-model="queryParams.maxCapacity"
          placeholder="请输入容纳总数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['day:meetingroom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['day:meetingroom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['day:meetingroom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['day:meetingroom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="meetingroomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="会议室名称" align="center" prop="meetingName" />
      <el-table-column label="会议编号" align="center" prop="meetingNumbering" />
      <el-table-column label="会议室内的特殊设施说明" align="center" prop="specialFacilitiesDescription" />
      <el-table-column label="会议室位置" align="center" prop="meetingLocation" />
      <el-table-column label="容纳总数" align="center" prop="maxCapacity" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="会议室状态" align="center" prop="status">
     <template #default="scope">
     <el-button
      :type="scope.row.status === 1 ? 'success' : 'danger'"
      @click="toggleStatus(scope.row)"
     >
      {{ scope.row.status === 1 ? '启用' : '禁用' }}
    </el-button>
    </template>
</el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" size="small" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['day:meetingroom:edit']">修改</el-button>
          <el-button link type="primary" size="small" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['day:meetingroom:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会议室信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="meetingroomRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会议室名称" prop="meetingName">
          <el-input v-model="form.meetingName" placeholder="请输入会议室名称" />
        </el-form-item>
        <el-form-item label="会议编号" prop="meetingNumbering">
          <el-input v-model="form.meetingNumbering" placeholder="请输入会议编号" />
        </el-form-item>
        <el-form-item label="会议室内的特殊设施说明" prop="specialFacilitiesDescription">
          <el-input v-model="form.specialFacilitiesDescription" placeholder="请输入会议室内的特殊设施说明" />
        </el-form-item>
        <el-form-item label="会议室位置" prop="meetingLocation">
          <el-input v-model="form.meetingLocation" placeholder="请输入会议室位置" />
        </el-form-item>
        <el-form-item label="容纳总数" prop="maxCapacity">
          <el-input v-model="form.maxCapacity" placeholder="请输入容纳总数" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Meetingroom">
import { listMeetingroom, getMeetingroom, delMeetingroom, addMeetingroom, updateMeetingroom } from "@/api/day/meetingroom";
import { ref } from 'vue'
const { proxy } = getCurrentInstance();

const meetingroomList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    meetingName: null,
    meetingNumbering: null,
    specialFacilitiesDescription: null,
    meetingLocation: null,
    maxCapacity: null,
    remarks: null,
    status: null
  },
  rules: {
    meetingName: [
      { required: true, message: "会议室名称不能为空", trigger: "blur" }
    ],
    meetingNumbering: [
      { required: true, message: "会议编号不能为空", trigger: "blur" }
    ],
    meetingLocation: [
      { required: true, message: "会议室位置不能为空", trigger: "blur" }
    ],
    maxCapacity: [
      { required: true, message: "容纳总数不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "会议室状态，0为禁用，1为启用不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会议室信息列表 */
function getList() {
  loading.value = true;
  listMeetingroom(queryParams.value).then(response => {
    meetingroomList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    meetingName: null,
    meetingNumbering: null,
    specialFacilitiesDescription: null,
    meetingLocation: null,
    maxCapacity: null,
    remarks: null,
    status: null
  };
  proxy.resetForm("meetingroomRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 切换会议室状态 */
function toggleStatus(row) {
  const newStatus = row.status === 1 ? 0 : 1;
  proxy.$modal.confirm(`确定要${newStatus === 1 ? '启用' : '禁用'}会议室 ${row.meetingName} 吗？`).then(() => {
    updateMeetingroom({ ...row, status: newStatus }).then(response => {
      proxy.$modal.msgSuccess(`${newStatus === 1 ? '启用' : '禁用'}成功`);
      getList();  // 刷新列表
    });
  }).catch(() => {
    proxy.$modal.msgError('操作取消');
  });
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会议室信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMeetingroom(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改会议室信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["meetingroomRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMeetingroom(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMeetingroom(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会议室信息编号为"' + _ids + '"的数据项？').then(function() {
    return delMeetingroom(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('day/meetingroom/export', {
    ...queryParams.value
  }, `meetingroom_${new Date().getTime()}.xlsx`)
}

getList();
</script>
